package controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import commons.MetronomeUtils;

import models.IMoteurMetronome;
import models.MoteurMetronome;
import views.IHM;

public class MoteurMetronomeController implements ActionListener, ChangeListener
{
	/*
	 * L'IHM de l'application
	 */
	private IHM _ihm;
	
	/*
	 * Le model du metronome
	 */
	private IMoteurMetronome _moteurMetronome;
	
	/*
	 * Constructeur
	 */
	public MoteurMetronomeController(IHM ihm)
	{ 
		_ihm = ihm;
		
		_moteurMetronome = new MoteurMetronome();
		
		initMeasure(MetronomeUtils.INITIAL_MEASURE);
		initTempo(MetronomeUtils.INITIAL_TEMPO);
	}
	
	private void initMeasure(int measure)
	{
		_moteurMetronome.setMeasure(measure);
	}
	
	private void initTempo(int tempo)
	{
		_moteurMetronome.setTempo(tempo);
		_ihm.getTempoSlider().setValue(tempo);
	}

	@Override
	public void stateChanged(ChangeEvent e) 
	{
		_moteurMetronome.setTempo(_ihm.getTempoSlider().getValue());
		_ihm.getTempoLabel().setText(Integer.toString(_ihm.getTempoSlider().getValue()));
	}

	@Override
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getActionCommand().equals(_ihm.getLaunchButton().getText()))
		{
			_ihm.getLaunchButton().setEnabled(false);
			_ihm.getStopButton().setEnabled(true);

			this.launch();
			
			checkButtons();
		}
		else if (e.getActionCommand().equals(_ihm.getStopButton().getText()))
		{
			_ihm.getLaunchButton().setEnabled(true);
			_ihm.getStopButton().setEnabled(false);
			_ihm.getIncMeasureButton().setEnabled(false);
			_ihm.getDecMeasureButton().setEnabled(false);
			
			this.stop();
		}
		else if (e.getActionCommand().equals(_ihm.getIncMeasureButton().getText()))
		{
			this.increment();

			checkButtons();
			
		}
		else if (e.getActionCommand().equals(_ihm.getDecMeasureButton().getText()))
		{
			this.decrement();
			
			checkButtons();
		}
	}
	
	private void launch()
	{
		System.out.println("DEMARRER");
		_moteurMetronome.setEtatMarche(true);
		System.out.println("Etat : " + _moteurMetronome.getEtatMarche());
		
		Clock.BattreTempo(cmdMarquerTempo, 100);
	}
	
	private void stop()
	{
		System.out.println("ARRETER");
		_moteurMetronome.setEtatMarche(false);
		System.out.println("Etat : " + _moteurMetronome.getEtatMarche());
	}
	
	private void increment()
	{
		if(isIncrementable())
		{
			_moteurMetronome.setMeasure(_moteurMetronome.getMeasure() + 1);
			System.out.println("INCREMENTER | Measure " + _moteurMetronome.getMeasure());
		}
	}
	
	private void decrement()
	{
		if(isDecrementable())
		{
			_moteurMetronome.setMeasure(_moteurMetronome.getMeasure() - 1);
			System.out.println("DECREMENTER | Measure " + _moteurMetronome.getMeasure());
		}
	}
	
	private boolean isDecrementable()
	{
		return _moteurMetronome.getMeasure() > 2;
	}
	
	private boolean isIncrementable()
	{
		return _moteurMetronome.getMeasure() < 7;
	}
	
	private void checkButtons()
	{			
		if(_moteurMetronome.getMeasure() == 7)
		{
			_ihm.getIncMeasureButton().setEnabled(false);
			_ihm.getDecMeasureButton().setEnabled(true);
		}
		else if(_moteurMetronome.getMeasure() == 2)
		{
			_ihm.getIncMeasureButton().setEnabled(true);
			_ihm.getDecMeasureButton().setEnabled(false);
		}
		else if(_moteurMetronome.getMeasure() > 2 || _moteurMetronome.getMeasure() < 7)
		{
			_ihm.getIncMeasureButton().setEnabled(true);
			_ihm.getDecMeasureButton().setEnabled(true);
		}
	}
}
